package cc.magicjson.example.demo.event;

import java.util.LinkedHashSet;
import java.util.Set;
import java.util.concurrent.Executor;

/**
 * {@link EventSource}
 *
 * @author <a href="mailto:it_magicjson@163.com">MagicJson</a>
 * @since 1.0.0
 */
public class CustomEventSource {

    private Set<CustomSpringApplicationEventListener> listenerSet = new LinkedHashSet<>();

    public void register(CustomSpringApplicationEventListener... listeners) {
        listenerSet.addAll(Set.of(listeners));
    }

    ;

    public void unregister(CustomSpringApplicationEventListener listener) {
        listenerSet.remove(listener);
    }

    ;

    public void fireEvent(CustomSpringApplicationEvent event) {
        listenerSet.forEach(listener -> listener.onEvent(event));
    }

    ;

    public static void fireEventAndNotifyAsync(CustomEventSource source,
                                               CustomSpringApplicationEvent event,
                                               Executor executor) {
        executor.execute(() -> source.fireEvent(event));

    }

    ;
}
